package com.assignwork.service.impl;

import com.assignwork.service.ICommonService;
import com.common.feign.ICommonServeClient;
import com.common.vo.CargoLocationDict;
import com.common.vo.CargoLocationInfoVO;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.user.DTO.UserDeptBizDTO;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springblade.system.user.vo.UserVO;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName CommonServiceImpl
 * @Description 通用接口实现
 * @Author dujianyu
 * @Date 2023/5/10 15:15
 */
@Service
@AllArgsConstructor
public class CommonServiceImpl implements ICommonService {

    private ICommonServeClient commonServeClient;
    private IUserClient userClient;

    @Override
    public String userName(String ids) {
        List<User> user = userClient.userInfoByIds(ids).getData();
        List<String> userNames = user.stream().map(User::getName).collect(Collectors.toList());
        return String.join(",", userNames);
    }

    @Override
    public Map<Long, String> positionMap() {
        CargoLocationDict data = commonServeClient.cargoLoactionR(null).getData();
        return data.getList().stream().collect(Collectors.toMap(CargoLocationInfoVO::getId, CargoLocationInfoVO::getName,  (key1, key2) -> key1 ));
    }

    @Override
    public Map<Long, Long> positionArea() {
        CargoLocationDict data = commonServeClient.cargoLoactionR(null).getData();
        return data.getList().stream().collect(Collectors.toMap(CargoLocationInfoVO::getId, CargoLocationInfoVO::getAreaId,  (key1, key2) -> key1 ));
    }


    public Map<String, String> areaMap() {
        return commonServeClient.areaSettingsMap();
    }
    @Override
    public List<UserVO> getUsers() {
        UserDeptBizDTO dto = new UserDeptBizDTO();
        dto.setUserId(AuthUtil.getUserId());
        dto.setSubsystemCode("tally");
        dto.setCode("tally");
        return userClient.getUsersList(dto).getData();
    }

    @Override
    public String getLocalTime17() {
        SimpleDateFormat format17 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return  format17.format(new Date());
    }

    @Override
    public String formatXMLString(String str) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(str));
            final Document document = db.parse(is);
            OutputFormat format = new OutputFormat(document);
            format.setLineWidth(65);
            format.setIndenting(true);
            format.setIndent(2);
            Writer out = new StringWriter();
            XMLSerializer serializer = new XMLSerializer(out, format);
            serializer.serialize(document);
            return out.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
